package com.kanshu.basictest.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.kanshu.basictest.R;

/**
 * 自定义View :
 * <p>
 * 实现方式：继承View,重写onDraw()方法
 */
public class CircleView extends View {

    public static final String TAG = "CircleView";

    //默认的填充颜色
    private int mColor;

    //默认的宽、高
    private int mWidth = 200;
    private int mHeight = 200;

    //画笔
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CircleView(Context context) {
        this(context, null);
    }


    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        //获取circle_color的属性值
        mColor = a.getColor(R.styleable.CircleView_circle_color, Color.RED);
        a.recycle();
        init();
    }

    private void init() {
        mPaint.setColor(mColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //计算控件宽、高的测量模式和测量值
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        //onMeasure()中不能使用getWidth()和getHeight()，两个结果都是0
        //  int width = getWidth();
        // int height = getHeight();

        //Log.i(TAG, "onMeasure: widthSpecMode:" + widthSpecMode + "-heightSpecMode:" + heightSpecMode);
        // Log.i(TAG, "onMeasure: widthSpecSize:" + widthSpecSize + "-heightSpecSize:" + heightSpecSize);
        //Log.i(TAG, "onMeasure: width:" + width + "-height:" + height);


        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, mHeight);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, heightSpecSize);

        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, mHeight);
        }


    }

    @Override
    protected void onDraw(Canvas canvas) {
        //View的onDraw()方法是空实现
        super.onDraw(canvas);


        //内边距
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();


        Log.i(TAG, "onDraw: paddingLeft:" + paddingLeft + "-paddingRight:" + paddingRight + "-paddingTop:" + paddingTop + "-paddingBottom:" + paddingBottom);

        //获取View的宽高
        int width = getWidth();
        int height = getHeight();

        //content的宽高
        int cWidth = getWidth() - paddingLeft - paddingRight;
        int cHeight = getHeight() - paddingTop - paddingBottom;

        //求半径
        int radius = Math.min(cWidth, cHeight) / 2;
        //绘制,(cx,cy)表示圆心坐标
        canvas.drawCircle(width / 2, height / 2, radius, mPaint);
    }
}
